package com.alibaba.citrus.ots.forecast.facade.service;

import com.alibaba.citrus.ots.common.annotation.FacadeInvoker;
import com.alibaba.citrus.ots.common.exception.FacadeException;
import com.alibaba.citrus.ots.forecast.repository.CartonSpecsRepository;
import com.alibaba.cz.base.tool.message.ErrorMessage;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.bcots.model.dto.EmployeeGetCurrentLoginResponse;
import com.epoch.app.bcots.model.dto.SearchScItemRequest;
import com.epoch.app.bcots.service.BaseDataService;
import com.epoch.app.bcots.service.BcOtsService;
import com.epoch.app.otsforecastsales.api.cartonspecswrite.dto.ExcelImportRequest;
import com.epoch.app.otsforecastsales.api.cartonspecswrite.service.CartonSpecsWriteService;
import com.epoch.app.otsforecastsales.cartonspecs.dto.CreateResponse;
import com.epoch.app.otsforecastsales.cartonspecs.model.CartonSpecs;
import com.epoch.app.otsforecastsales.cartonspecs.service.CartonSpecsService;
import com.epoch.app.otsforecastsales.model.dto.CartonSepcsRequest;
import com.epoch.app.otsforecastsales.model.dto.CartonSpecsSDO;
import com.epoch.app.otsplatformenhance.api.scitemquery.service.ScItemQueryService;
import com.epoch.app.otsplatformenhance.dto.request.IdQueryRequest;
import com.epoch.app.otsplatformenhance.model.dto.ScItemSDO;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @description:
 * @author: qianlei
 * @create: 2021/11/11 22:36
 */
@Component
@Primary
public class CartonSpecsWriteServiceImpl implements CartonSpecsWriteService {
    @Autowired
    CartonSpecsRepository cartonSpecsRepository;
    @Autowired
    ScItemQueryService scItemQueryService;
    @Autowired
    private CartonSpecsService cartonSpecsService;
    @Autowired
    private BcOtsService bcOtsService;

    @Autowired
    private BaseDataService baseDataService;

    @Override
    @FacadeInvoker
    public Result<String> saveCartonSpecs(CartonSepcsRequest cartonSepcsRequest){
        CartonSpecsSDO cartonSpecsSDO = new CartonSpecsSDO();
        cartonSpecsSDO.setScItemId(cartonSepcsRequest.getScItemId());
        CartonSepcsRequest sepcsRequest = CartonSepcsRequest.newCartonSepcsRequestBuilder()
                .scItemId(cartonSepcsRequest.getScItemId())
                .build();
        List<CartonSpecsSDO> cartonSpecsSDOS = cartonSpecsRepository.queryList(sepcsRequest);
        if (CollectionUtils.isNotEmpty(cartonSpecsSDOS)){
            throw  new FacadeException("OTS-04-001-00-15-020");
        }
        IdQueryRequest idQueryRequest = new IdQueryRequest();
        idQueryRequest.setId(cartonSepcsRequest.getScItemId());
        Result<ScItemSDO> result = scItemQueryService.getScItemById(idQueryRequest);
        if (result == null || result.getResult() == null) {
            throw  new FacadeException("OTS-04-001-00-15-020");
        }
        ScItemSDO scItemSDO = result.getResult();
        cartonSpecsSDO.setOutScItemCode(scItemSDO.getOuterId());
        cartonSpecsSDO.setScItemTitle(scItemSDO.getTitle());
        cartonSpecsSDO.setSpecs(cartonSepcsRequest.getSpecs());
        cartonSpecsSDO.setGmtCreate(new Date());
        cartonSpecsSDO.setGmtModified(new Date());

        String id = cartonSpecsRepository.save(cartonSpecsSDO);

        return Result.success(id);
    }

    @Override
    @FacadeInvoker
    public Result<String> modifySpecs(CartonSepcsRequest cartonSepcsRequest){
        IdQueryRequest idQueryRequest = new IdQueryRequest();
        idQueryRequest.setId(cartonSepcsRequest.getScItemId());
        Result<ScItemSDO> result = scItemQueryService.getScItemById(idQueryRequest);
        if (result == null || result.getResult() == null) {
            throw  new FacadeException("未查询货品");
        }
        Long count = cartonSpecsRepository.modify(cartonSepcsRequest);

        if (count != null) {
            return Result.success(count.toString());
        }
        return Result.success(null);
    }

    @Override
    @FacadeInvoker
    public Result<Void> deleteCartonSpecs(CartonSepcsRequest cartonSepcsRequest){
        Long count = cartonSpecsRepository.delete(cartonSepcsRequest);
//        if (count != null) {
//            return Result.success(null);
//        }
        return Result.success(null);
    }

    @Override
    public Result<List<CartonSpecsSDO>> pageQuery(CartonSepcsRequest cartonSepcsRequest){
        List<CartonSpecsSDO> cartonSpecsSDOS = cartonSpecsRepository.queryList(cartonSepcsRequest);
        if (cartonSpecsSDOS == null){
            return Result.listEmptySuccess();
        }
        return Result.listSuccess(cartonSpecsSDOS,cartonSpecsSDOS.size());
    }

    @Override
    public Result<List<CartonSpecsSDO>> excelImport(ExcelImportRequest excelImportRequest){

        if (Objects.isNull(excelImportRequest)||CollectionUtils.isEmpty(excelImportRequest.getCartonSpecList())){
            return Result.fail("","导入内容为空");
        }

        EmployeeGetCurrentLoginResponse employeeGetCurrentLoginResponse = bcOtsService.employeeGetCurrentLogin();
        if (Objects.isNull(employeeGetCurrentLoginResponse)){
            throw new FacadeException("获取当前登录信息异常");
        }
        List<CartonSpecsSDO> cartonSpecList = excelImportRequest.getCartonSpecList();
        List<CartonSpecsSDO> responseList=new ArrayList<>();

        for (CartonSpecsSDO cartonSpecsSDO : cartonSpecList) {
            CartonSpecs cartonSpecs=new CartonSpecs();

            if (StringUtils.isBlank(cartonSpecsSDO.getOutScItemCode())){
                cartonSpecsSDO.setErrorMessage("SAP编码不能为空");
                continue;
            }

            if (StringUtils.isBlank(cartonSpecsSDO.getScItemTitle())){
                cartonSpecsSDO.setErrorMessage("货品名称不能为空");
                responseList.add(cartonSpecsSDO);
                continue;
            }

            if (null==cartonSpecsSDO.getSpecs()||0==cartonSpecsSDO.getSpecs()){
                cartonSpecsSDO.setErrorMessage("运输规格不能为空");
                responseList.add(cartonSpecsSDO);
                continue;
            }

            SearchScItemRequest searchScItemRequest = new SearchScItemRequest();
            HashMap<String, List<String>> hashMap = new HashMap<>();
            hashMap.put("outerIdList",Collections.singletonList(cartonSpecsSDO.getOutScItemCode()));
            searchScItemRequest.setSearchReqDTO(hashMap);
            Result<List<com.epoch.app.bcots.model.dto.ScItemSDO>> scItem = baseDataService.searchScItem(searchScItemRequest);
            if (Objects.isNull(scItem)||CollectionUtils.isEmpty(scItem.getResult())){
                cartonSpecsSDO.setErrorMessage("查询不到内部货品编码");
                responseList.add(cartonSpecsSDO);
                continue;
            }
            cartonSpecsSDO.setScItemId(scItem.getResult().get(0).getScItemId());
            cartonSpecsSDO.setCreator(employeeGetCurrentLoginResponse.getEmployeeId());
            cartonSpecsSDO.setCreatorName(employeeGetCurrentLoginResponse.getEmployeeName());
            cartonSpecsSDO.setModifier(employeeGetCurrentLoginResponse.getEmployeeId());
            cartonSpecsSDO.setModifierName(employeeGetCurrentLoginResponse.getEmployeeName());
            cartonSpecsSDO.setGmtModified(new Date());
            cartonSpecsSDO.setGmtCreate(new Date());

            BeanUtils.copyProperties(cartonSpecsSDO,cartonSpecs);
            CreateResponse createResponse = cartonSpecsService.create(cartonSpecs);
            if (Objects.nonNull(createResponse)&& StringUtils.isNotBlank(createResponse.getId())){
                cartonSpecsSDO.setId(Long.valueOf(createResponse.getId()));
            }else{
                cartonSpecsSDO.setErrorMessage("储存数据库失败");
            }
            responseList.add(cartonSpecsSDO);
        }

       return Result.success(responseList);

    }



}
